home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10188 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: ip058.phx.primenet.com!sundial
  2. From: sundial@primenet.com (Sundial Services)
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: 6 Mar 1996 07:59:02 -0700
  6. Organization: Primenet
  7. Sender: root@primenet.com
  8. Distribution: world
  9. Message-ID: <sundial.2503.0035ADFA@primenet.com>
  10. References: <4fr8be$ass@news.iconn.net> <4g2agv$29r@clarknet.clark.net> <4gsn1d$ia@epervier.CC.UMontreal.CA>
  11. X-Posted-By: ip058.phx.primenet.com
  12. X-Newsreader: Trumpet for Windows [Version 1.0 Rev B final beta #4]
  13.  
  14. In article <4gsn1d$ia@epervier.CC.UMontreal.CA> pigeons@JSP.UMontreal.CA (PIGEON Steven) writes:
  15.  
  16. >Harlan Messinger (gusty@clark.net) wrote:
  17. >: void getLastFactorialDigits(int results[], // assume sufficient space
  18. >:                                            // was preallocated (n+1 bytes)
  19. >:                               int n) {
  20. >: 
  21. >:       results[0] = 1;
  22. >:       results[1] = 1;
  23. >:       for (int i = 2; i <= n; ++i) {
  24. >:               results[i] = results[i - 1] * i;
  25. >:               while (results[i] % 10 == 0)
  26. >:                       results[i] /= 10;
  27. >:               results[i] = results[i] % 10;
  28. >:       }
  29. >:       return;
  30. >: }
  31.  
  32.  
  33. >Ah a! In PASCAL!!! :-) This is a Pascal group!
  34.  
  35.  
  36.  
  37. > function zerofacto(n:integer):integer;
  38. > var s,j,i:longint;
  39. >  begin
  40. >   s:=1;
  41. >   j:=1;
  42. >   i:=0;
  43.  
  44. >   while (j<=n) do
  45. >    begin
  46. >     s:=s*j;
  47. >     while ( (s>10) and (s mod 10=0)) do
  48. >      begin
  49. >       s:=s div 10;
  50. >       inc(i);
  51. >      end;
  52. >     s:=s mod 10;
  53. >     inc(j);
  54. >    end;
  55. >    zerofacto:=s mod 10;
  56. >  end;
  57.  
  58.  
  59.  
  60. Since there are less than 69 factorials that can be represented in most real 
  61. number systems, this would be a great place for a table-lookup.  Calculate 
  62. them once and store them in a constant array.
  63.